home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: netcom.com!marnold
- From: marnold@netcom.com (Matt Arnold)
- Subject: Re: Aborting Constructors, Part II
- Message-ID: <marnoldDMCK32.66r@netcom.com>
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
- References: <4eoeck$t6n@news.ios.com> <4eq99t$fpu@fsuj01.rz.uni-jena.de> <marnoldDM529E.3CH@netcom.com> <mikec.823353055@atl1>
- Date: Tue, 6 Feb 1996 09:09:01 GMT
- Sender: marnold@netcom17.netcom.com
-
- mikec@atl1.america.net (Michael J. Callahan) writes:
-
- >>>Problem 2: The language already provides an automatic call to delete
- >>>if you throw an exception from an object that was allocated using new.
- >>>The code above would cause delete to be called twice! Once again, heap
- >>>corruption is likely.
- >>>
-
- >Now how did I miss that? Through my reading I try to keep abreast of
- >stds, as they evolve, but I profess ignorance of the above point. Could
- >you elaborate on this a little more?
-
- (Well, it looks like you outlined it pretty well in your following text.)
-
- >It sounds as if you are saying that the raw memory allocated for the
- >ctor to be run against is automatically freed by virtue of an exception
- >being thrown within a ctor.
-
- That's what I'm saying.
-
- >Of course this seems to mean that exceptions
- >have to treat their invocation differently from within ctors, as versus
- >other placement positions in code, which implies more knowledge than I
- >thought an exception had. It also seems to imply that once you have thrown
- >an exception in a ctor, that object no longer exists (if indeed the raw
- >memory for the object has automatically and unconditionly been deallocated).
- >I must confess I have never tried to test/access an object that threw an
- >exception in its ctor to see whether the object was just an invalid instance
- >or actually no longer existed.
-
- Conceptually, if your throw an exception in the midst of constructing an
- object, it is "deconstructed" and, in a way, never existed. The language
- even provides a way for all traces of failed dynamically allocated objects
- to be removed, the memory allocated for them being automatically returned
- to the heap.
-
- It should be noted that, before this addition to the language, throwing a
- exception in the ctor of a dynamically allocated object caused a memory
- leak! There was no (portable, or even palatable) way to recover the leaked
- memory. Only the language itself could plug the leak.
-
- For example (assuming the auto-delete feature were not implemented) how
- would you recover the memory leaked by the following code fragement (and it
- *is* leaked)? Assume the memory allocation succeeds but Object::Object()
- throws some kind of exception...
-
- Object* p;
-
- try
- {
- p = new Object;
- }
- catch (...)
- {
- // now what?
- }
-
-
- Hint: You can't recover it. The main problem is that p never gets
- initialized. There's nothing in the catch block you can do to find what
- value new returned.
-
- >So under the covers, a new allocates raw memory for the object to occupy,
- >and begins running the ctor on that raw memory. Within the ctor, if an
- >exception is thrown, the raw memory allocation is deallocated, the instance
- >in effect no longer exists?
-
- Yup.
-
- >Is that recent behavior to the evolving std, or did I just miss it? If
- >recent, is it implemented in many compilers yet?
-
- It's not *that* recent. Probably not implemented in the half-baked C++
- compilers yet. All I know is that Borland C++ has been doing since version
- 4.0 or so.
-
- Regards,
- -------------------------------------------------------------------------
- Matt Arnold | | ||| | |||| | | | || ||
- marnold@netcom.com | | ||| | |||| | | | || ||
- Boston, MA | 0 | ||| | |||| | | | || ||
- 617.389.7384 (h) 617.576.2760 (w) | | ||| | |||| | | | || ||
- C++, MIDI, Win32/95 developer | | ||| 4 3 1 0 8 3 || ||
- -------------------------------------------------------------------------
-